const app = getApp();
import services from '../../services/clockin';
import common from '../../utils/common';

Page({
  data: {
    isReady: false,
    pendList: [],
    rejectList: [],
    resolveList: [],
    showList: [],
    mode: 0, // 0待审批,1审批通过,2审批驳回
    modeOptions: [{
      title: "待审批",
    }, {
      title: "审批通过"
    }, {
      title: "审批驳回"
    }],
    unionid: '',
    isLoading: false,
    pageParams: {
      page: 1,
      size: 10000,
      order_by: 'clock_datetime'
    },
    confirmDialog: {
      visible: false,
      info: null,
      type: 1,
      value: '',
    }
  },
  async onLoad() {
    const {
      unionid
    } = app.globalData.userInfo || {};
    this.setData({
      unionid: unionid || ''
      // 2QV8HeciS9zgYhRAtxbpiPZwiEiE
    })
    await this.queryGetApproveOuting();
    this.setData({
      isReady: true
    })
  },
  handleChangeMode(current) {
    this.setData({
      mode: current || 0
    })
    switch (current) {
      case 0: {
        this.setData({
          showList: this.data.pendList,
        })
        break;
      }
      case 1: {
        this.setData({
          showList: this.data.rejectList,
        })
        break;
      }
      case 2: {
        this.setData({
          showList: this.data.resolveList,
        })
        break;
      }
    }

  },
  async queryGetApproveOuting() {
    try {
      const pMap = [{
        ...this.data.pageParams,
        is_approve: 0
      }, {
        ...this.data.pageParams,
        is_approve: 1
      }, {
        ...this.data.pageParams,
        is_approve: 2
      }]
      const [res1, res2, res3] = await Promise.all([services.getApproveAddition(pMap[0], this.data.unionid), services.getApproveAddition(pMap[1], this.data.unionid), services.getApproveAddition(pMap[2], this.data.unionid)]);
      // 0:未通过 1:已通过 2: 驳回
      if (res1 && res1.data) {
        this.setData({
          pendList: res1.data || []
        })
      }
      if (res2 && res2.data) {
        this.setData({
          rejectList: res2.data || []
        })
      }
      if (res3 && res3.data) {
        this.setData({
          resolveList: res3.data || []
        })
      }
      console.log('waitList', this.data, res1, res2, res3);
    } catch (error) {
      console.log("获取外勤待审批列表失败", error);
    } finally {
      this.handleChangeMode(this.data.mode)
    }
  },
  async handlePrimaryButtonTap() {
    const {
      info,
      type,
      value
    } = this.data.confirmDialog
    console.log(1, this.data.confirmDialog)
    if (!value) {
      dd.showToast({
        type: 'info',
        content: `请填写${type===1?'通过':'拒绝'}原因`,
        duration: 1000
      });
      return;
    }
    const {
      id
    } = info
    if (this.data.isLoading || !id) {
      return;
    }
    this.setData({
      isLoading: true
    });
    const params = {
      accept: type === 1,
      suggestion: value
    }
    await this.queryPutApproveOuting(id, params);
    this.setData({
      isLoading: false,
      confirmDialog: {
        info: {},
        value: '',
        type: 1,
        visible: false
      }
    });
  },
  async queryPutApproveOuting(id, params) {
    try {
      console.log('提交外勤打卡', id, params);
      const res = await services.putApprovAddition(id, params, this.data.unionid);
      await this.queryGetApproveOuting();
      console.log('提交外勤打卡', id, params, res);
      dd.showToast({
        type: 'success',
        content: '操作成功',
        duration: 2000
      });
      this.setData({
        isLoading: false
      });
    } catch (error) {
      this.setData({
        isLoading: false
      });
      common.showErrorToast(error);
    }
  },
  handleChangeInput(value) {
    this.setData({
      confirmDialog: {
        ...this.data.confirmDialog,
        value
      }
    });
  },
  handleSecondaryButtonTap() {
    this.handleClose()
  },
  handleClose() {
    this.setData({
      confirmDialog: {
        visible: false,
        info: null,
        value: '',
      }
    })
  },
  handleApprove(e) {
    if (this.data.isLoading) {
      return;
    }
    const {
      item,
      type
    } = e.currentTarget.dataset || {}
    this.setData({
      confirmDialog: {
        visible: true,
        info: item,
        type: type || 1,
        value: ''
      }
    })
  }
});